//双向链表
class ListNode {
    public int val;
    public ListNode next;
    public ListNode prev;

    public ListNode(int val){
        this.val = val;
    }
}

public class DoubleLinkedList {
    public ListNode head;
    public ListNode last;

    //打印链表
    public void display(){
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        //要先判断是否为空
        ListNode cur = new ListNode(data);
        if (this.head == null){
            this.head = cur;
            this.last = cur;
        }else {
            cur.next = this.head;
            this.head.prev = cur;
            this.head = cur;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if (this.head == null){
            this.head = cur;
            this.last = cur;
        }else {
            cur.prev = this.last;
            this.last.next = cur;
            this.last = cur;
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                if (cur == this.head){
                    this.head = this.head.next;
                    //别忘了把head的前驱设置为null
                    if (head != null){
                        this.head.prev = null;
                    }else {
                        this.last = null;
                    }
                }else if (cur == this.last){
                    cur.prev.next = cur.next;
                    this.last = this.last.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }
    public void remove_1(int key){
        ListNode cur = this.head;
        while (cur.val != key){
            cur = cur.next;
            if (cur == null){
                return;
            }
        }
        if (cur == this.head){
            this.head = this.head.next;
            if (head != null){
                this.head.prev = null;
            }else {
                this.last = null;
            }
        }else if (cur == this.last){
            cur.prev.next = cur.next;
            this.last = this.last.prev;
        }else {
            cur.prev.next = cur.next;
            cur.next.prev = cur.prev;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public ListNode searchIndex(int index){
        ListNode cur = this.head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    public void addIndex(int index,int data){
        if (index < 0 || index > size()){
            return;
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode cur = searchIndex(index);
        ListNode node = new ListNode(data);
        node.prev = cur.prev;
        node.next = cur;
        //注意这两个顺序
        cur.prev.next = node;
        cur.prev = node;
    }


    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                if (cur == this.head){
                    //要变头节点，一定要head作为赋值对象，双向链表别忘了prev设为null
                    this.head = this.head.next;
                    //如果链表只有一个值，删了会空
                    if (head != null){
                        this.head.prev = null;
                    }else {
                        this.last = null;
                    }
                }else if (cur == this.last){
                    //同理，last为赋值对象，next设置为null
                    cur.prev.next = cur.next;
                    this.last = this.last.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    //清空链表
    public void clear(){
        while (head != null){
            //需要定义在循环内，防止head为空
            ListNode headNext = head.next;
            head.next = null;
            head.prev = null;
            head = headNext;
        }
        last = null;
    }
}

//1、顺序表和链表的区别
//2、数组和链表的区别
//3、ArrayList和LinkedList的区别
/*这三个是同一个问题*/
/*
 * 组织上：
 * 1)顺序表底层是一个数组，逻辑上和物理上都是连续的
 * 2)链表是由若干个节点组成的一个数据结构，逻辑上连续，物理上（内存上）不连续
 */

/*
 * 操作上：
 * 1)顺序表适合查找，用下标获取某个位置元素
 * 2)链表适合与插入与删除，不用像顺序表一样移动元素，只需要修改指向
 * 3)顺序表有时需要扩容，扩的容有可能造成浪费，空间利用率低
 */